package org.example.maven.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.example.maven.annotation.ReadOnlyMode;
import org.example.maven.throwing.demoMode.DemoModelException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.nio.file.AccessDeniedException;

@Component
@Aspect
public class EnableReadModeAspect {
    // 从配置文件中读取是否启用演示模式
    @Value("${app.demo-mode.enabled}")
    private boolean isDemoMode;

    // 或直接拦截自定义注解 @ReadOnlyMode
    @Around("@annotation(readOnlyMode)")
    public Object checkReadOnlyMode(ProceedingJoinPoint joinPoint, ReadOnlyMode readOnlyMode) throws Throwable {
        if (isDemoMode) {
            // 抛出自定义异常，携带提示信息
            throw new DemoModelException("演示模式不允许操作");
        }
        return joinPoint.proceed();
    }
}
